<template>
  <div class="container tree-list-layout">
        <treeOffice @click="handleTreeClick" ref="treeOffice" class="box-left"/>

        <data-view
          ref="userGrid"
          propViewKey="sys_user"
          propViewForm="sys/user/detail.vue"
          :propInitData="selectedNode"
          @after-save="handleAfterSave"
          @after-delete="handleAfterDelete"
          @extra-button-click="extraButtonClick"
          @before-button-click="beforeButtonClick"
          :propBeforeClick="propBeforeClickDtl"
          :propIsTdesign="true"
          class="box-right"
          :propTableOpWidth="250"
        />
        <el-drawer title="设置用户库位权限"
          :append-to-body="true"
          :visible.sync = "setWarehouseVisible"
          direction="rtl"
          size="80%">
          <UserWareHouse ref="warehouseForm"
          :propInitData="warehouseInitData"
          @formClose="whFormClose"
         :propSetTypeField="'user_id'"
          />
        </el-drawer>



    <!--数据导入-->
    <customDrawer title="用户数据导入" ref="customDrawerImport"
                  :widthPx="500" :heightPx="500"
                  hideConfirm
    >
      <template #content>
        <dataImportArea
          downloadPath="/user/excelDownloadTemp"
          importPath="/user/excelImport"
          :sheetName="'用户'"
          helpMsg="
          1、【下载模板】维护好数据 -> 【选择文件】导入数据<br>
          2、<span class='span-msg-nopad'>目前仅新增用户</span><br>
          3、<span class='span-msg-nopad'>用户工号必须唯一</span><br>
          4、<span class='span-msg-nopad'>用户机构必须存在</span><br>
          5、<span class='span-msg-nopad'>姓名、工号、机构编码必填</span>
"
          @success="menuActiveCall"
          ref="dataImportArea"
        />
      </template>
    </customDrawer>
  </div>
</template>

<script>
import treeOffice from '@/components/custom/bus/treeOffice.vue'
import UserWareHouse from '@/views/sys/user-warehouse/detail.vue'
import dataImportArea from '@/components/custom/bus/dataImportArea.vue'

export default {
  name: 'sysUser',
  data() {
    return {
      selectedNode: {
        value: 0,
        parentId: -1,
        label: '全部机构'
      },
      warehouseInitData: {},
      setWarehouseVisible: false
    }
  },
  created() {
  },
  components: {
    UserWareHouse,
    treeOffice,
    dataImportArea
  },
  watch: {
  },
  methods: {
    // 菜单激活触发
    menuActiveCall() {
      this.$refs.treeOffice.load()
      this.$refs.userGrid.fetchData()
    },
    handleTreeClick(data) {
      this.selectedNode = data
      let wrapper = data.id == 0 ? this.$newWrapper() : this.$newWrapper().eq('officeId', data.id)
      this.$refs.userGrid.setQueryWrapper(wrapper)
    },
    handleAfterSave() {
      this.$refs.treeOffice.load()
    },
    handleAfterDelete() {
      this.$refs.treeOffice.load()
    },
    extraButtonClick({button, data}) {
      let buttonKey = button.key
      if (buttonKey === 'setWarehouse') {
        this.warehouseInitData = data
        this.setWarehouseVisible = true
        this.$nextTick(() => {
          this.$refs.warehouseForm.initData()
        })
      }
      if (buttonKey === 'resetPwd') {
        this.$confirm('此操作将重置用户密码, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$ajax.post('/user/resetPwd', {
            id: data.id
          }).then(resp => {
            resp.success && this.utilMessage.success(resp.msg)
          })
        }).catch(() => {
          this.utilMessage.info('已取消')
        })
      }
      if (buttonKey === 'syncUser') {
        this.utilMessage.confirm('根据HIS视图数据，同步用户信息，是否确认?' , () => {
          this.utilVue.loadShow(true)
          this.$ajax.post('his/syncUser').then(response => {
            this.utilVue.loadHide()
            if (response.code == 0) {
              this.utilMessage.success(response.msg)
              this.menuActiveCall()
            }
          })
        })

      }
      if (buttonKey === 'importDataCustom') { // 导入数据
        this.$refs.dataImportArea.clear()
        this.$refs.customDrawerImport.show()
      }
    },
    //按钮操作前
    beforeButtonClick({btn, call}) {
    },
    whFormClose() {
      this.setWarehouseVisible = false
    },
    // 列表按钮点击前操作
    propBeforeClickDtl(button) {
      const _selectedData = this.selectedNode
      //列表添加按钮
      if (button.key === "vb-add") {
        if(!_selectedData || !_selectedData.id ||  _selectedData.id === "0") {
          this.utilMessage.warning('请选择下级机构')
          return false
        }
      }
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
  .container {
    height: 100%;
    background-color: #fff;
  }
</style>
